package com.lijin.batchsys.framework.dao;

import java.net.URL;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.lijin.batchsys.framework.common.AssertUtil;
import com.lijin.batchsys.framework.common.BatchSysException;

/**
 * 
 * Description: 
 *
 * (C) Copyright of China UnionPay Co., Ltd. 2010.
 *
 */
public class BatchSysSQL {
	
	private static final Logger logger = Logger.getLogger(BatchSysSQL.class);

	private static final BatchSysSQL _INSTANCE = new BatchSysSQL();
	static {
		_INSTANCE.init();
	}
	
	public static final String SQL_SELECT_ALL_BATCH = "SELECT_ALL_BATCH";
	public static final String SQL_SELECT_ALL_JOB = "SELECT_ALL_JOB";
	public static final String SQL_SELECT_ALL_STEP = "SELECT_ALL_STEP";
	public static final String SQL_INSERT_BATCH_EXECLOG = "INSERT_BATCH_EXECLOG";
	public static final String SQL_UPDATE_BATCH_EXEC_LOG = "UPDATE_BATCH_EXEC_LOG";
	public static final String SQL_SEL_BATCH_EXECLOG_BY_ID = "SEL_BATCH_EXECLOG_BY_ID";
	public static final String SQL_SEL_ALL_BATCH_EXECLOG = "SEL_ALL_BATCH_EXECLOG";
	public static final String SQL_INSERT_BATCH_EXECLOG_DETAIL = "INSERT_BATCH_EXECLOG_DETAIL";
	public static final String SQL_SEL_BATCH_EXECLOG_DETAIL_BY_ID = "SEL_BATCH_EXECLOG_DETAIL_BY_ID";
	public static final String SQL_SEL_BATCH_EXECLOG_DETAIL_BY_BATCH_LOG_ID = "SEL_BATCH_EXECLOG_DETAIL_BY_BATCH_LOG_ID";
	public static final String SQL_UPDATE_BATCH_EXEC_LOG_DETAIL = "UPDATE_BATCH_EXEC_LOG_DETAIL";
	public static final String SQL_SEL_COUNT_DETAIL_LOGID = "SEL_COUNT_DETAIL_LOGID";
	
	private Map<String, String> sqlMap = new Hashtable<String, String>();
	
	public static BatchSysSQL getInstance() {
		return _INSTANCE;
	}
	
	@SuppressWarnings("unchecked")
	private void init() {
		try {
			URL url = BatchSysSQL.class.getClassLoader().getResource("batchSQL_db2.xml");
			AssertUtil.objIsNull(url, "SQL配置文件不存在:batchSQL_db2.xml");
			SAXReader reader = new SAXReader();
			Document document = reader.read(url);
			Element root = document.getRootElement();
			if (!root.getName().equals("batchSQL")) {
				throw new BatchSysException("配置文件格式不合法，根目录必须是:batchSQL");
			}
			
			for (Iterator i = root.elementIterator("sql"); i.hasNext();) {
	            Element sql = (Element) i.next();
	            sqlMap.put(sql.attributeValue("id"), sql.getStringValue().trim());
	        }

			// 打印从配置文件读取的SQL语句
			StringBuilder sqlConfig = new StringBuilder("SQL_CONFIG:\n{\n");
			for (String k : sqlMap.keySet()) {
				sqlConfig.append(k + "=" + sqlMap.get(k) + "\n");
			}
			sqlConfig.append("}\n");
			logger.info(sqlConfig.toString());
		} catch (DocumentException e) {
			logger.error("读取SQL语句配置文件失败", e);
		}
	}
	
	/**
	 * 
	 * @param sqlKey
	 * @return
	 */
	public String fetchSQL(String sqlKey) {
		if (sqlMap.isEmpty()) {
			throw new BatchSysException("SQL配置信息未初始化");
		}
		if (sqlMap.get(sqlKey) == null) {
			
		}
		return sqlMap.get(sqlKey);
	}
}
